#include "volumebutton.h"
#include <QSlider>
#include <QMouseEvent>
#include <QtDebug>
#include "cvolumesliderdlg.h"

VolumeButton::VolumeButton(QWidget *parent) : MyPushButton(parent)
{
    this->m_pSliderDlg = new CVolumeSliderDlg(this);
    m_mute = false;
    m_timerId = -1;
    connect(this,&VolumeButton::init_volume,m_pSliderDlg,&CVolumeSliderDlg::setSliderValue);
}

VolumeButton::~VolumeButton(){
    //disconnect()
}

void VolumeButton::on_VolumeChanged(int value){
    emit volume_changed(value);
}

void VolumeButton::enterEvent(QEvent *event){
    showSlider(event);
    if(m_timerId == -1){
        m_timerId = startTimer(250);
    }
    connect(m_pSliderDlg,&CVolumeSliderDlg::sig_SliderValueChanged,this,&VolumeButton::on_VolumeChanged);
    //connect(m_pSliderDlg,)
    MyPushButton::enterEvent(event);
}

void VolumeButton::setVolume(int volume){
    emit init_volume(volume);
}

void VolumeButton::setMute(bool mute){
    emit init_volume(0);
}

void VolumeButton::timerEvent(QTimerEvent *event){
    if((m_pSliderDlg != nullptr) && (m_pSliderDlg->isVisible())){
        QPoint mousePoint = QCursor::pos();
        QRect rect1 = this->rect(); //获取声音按钮的矩形区域

        QRect rect2 = m_pSliderDlg->rect();//获取滑块对象的矩形区域
        QRect rect3 = m_pSliderDlg->geometry();//获取滑块对象的几何信息

        qDebug() << "rect1 left=" <<rect1.left() <<",top=" << rect1.top() << ",width=" << rect1.width() << ",height=" << rect1.height() << endl;
        qDebug() << "rect2 left=" <<rect2.left() <<",top=" << rect2.top() << ",width=" << rect2.width() << ",height=" << rect2.height() << endl;
        qDebug() << "rect3 left=" <<rect3.left() <<",top=" << rect3.top() << ",width=" << rect3.width() << ",height=" << rect3.height() << endl;

        QPoint p2 = this->mapToGlobal(QPoint(0,0));
        qDebug() << "p2 x=" << p2.x() << ",y=" << p2.y() << endl;
        QRect area(rect3.left(),rect3.top(),rect2.width(),p2.y()+rect1.height() -rect3.top());
        if(!area.contains(mousePoint)){
            m_pSliderDlg->hide();
        }
    }else{
        killTimer(m_timerId);
        m_timerId = -1;
    }
}

void VolumeButton::leaveEvent(QEvent *event){
    //m_pSliderDlg->hide();
    MyPushButton::leaveEvent(event);
}

void VolumeButton::mousePressEvent(QMouseEvent* ev){
    if(ev->button() == Qt::LeftButton){
        m_mute = !m_mute;
        if(m_mute){
            if(m_pSliderDlg){
                m_pSliderDlg->setSliderValue(0);
            }
        }else{
            if(m_pSliderDlg){
                m_pSliderDlg->setSliderValue(50);
            }
        }
    }
    MyPushButton::mousePressEvent(ev);
}

void VolumeButton::showSlider(QEvent *event){
    //获取音量按钮左上角相对于整个界面的位置
    QPoint p1 = this->mapToGlobal(QPoint(0,0));
    QRect btnRect = this->rect();

    //获取滑块对象的矩形区域，rect包含标题栏，去掉标题栏后高度不变
    QRect sliderRect = m_pSliderDlg->rect();

    //计算滑块对象在水平方向上的位置，使其水平居中于声音按钮
    int x = p1.x() + (btnRect.width() - sliderRect.width())/2;

    //计算滑块对象在垂直方向上的位置,使其位于声音按钮上方并略微偏移5px
    int y = p1.y() - sliderRect.height() - 5;

    //滑块对象移动到计算得到的位置
    m_pSliderDlg->move(x,y);

    m_pSliderDlg->show();

}
